package com.coc.action;

import java.security.MessageDigest;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts.actions.DispatchAction;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.coc.dao.BuildSettingXML;
import com.coc.dao.EloUserDAO;
import com.coc.dao.MemCachedDAO;
import com.coc.service.CocBattleService;
import com.coc.service.CocMapService;
import com.coc.service.CocUserService;
import com.coc.util.MemCacheUtil;

public class BaseAction extends DispatchAction {
	public Integer[][] SetMap(Integer[][] map, int x, int y, int size) {
		for (int i = 0; i < size; i++) {
			for (int j = 0; j < size; j++) {
				map[(x - 1 + i)][(y - 1 + j)] = Integer.valueOf(1);
			}
		}
		return map;
	}

	public CocUserService getCocUserService() {
		return (CocUserService) getBean("cocUserService");
	}

	public CocMapService getCocMapService() {
		return (CocMapService) getBean("cocMapService");
	}

	public CocBattleService getCocBattleService() {
		return (CocBattleService) getBean("cocBattleService");
	}

	public BuildSettingXML getBuildSettingXML() {
		return (BuildSettingXML) getBean("buildSettingXML");
	}

	public EloUserDAO getEloUserDAO() {
		return (EloUserDAO) getBean("eloUserDAO");
	}

	public MemCachedDAO getMemCachedDAO() {
		return (MemCachedDAO) getBean("memCachedDAO");
	}

	public Object getBean(String name) {
		ApplicationContext ctx = WebApplicationContextUtils
				.getRequiredWebApplicationContext(this.servlet
						.getServletContext());
		return ctx.getBean(name);
	}

	public boolean isLoginV(HttpServletRequest request) {
		return true;
	}

	public String MD5digest(String message) {
		char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'a', 'b', 'c', 'd', 'e', 'f' };
		try {
			byte[] strTemp = message.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char[] str = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[(k++)] = hexDigits[(byte0 >>> 4 & 0xF)];
				str[(k++)] = hexDigits[(byte0 & 0xF)];
			}
			return new String(str);
		} catch (Exception e) {
		}
		return null;
	}

	public boolean isLogin(HttpServletRequest request) {
		String tk = request.getParameter("tk");
		if (tk == null)
			return false;
		try {
			int uid = Integer.parseInt(tk.substring(0, tk.indexOf("_")));
			String sessionString = MemCacheUtil.getSession(uid);
			String token = tk.substring(tk.indexOf("_") + 1);
			if (sessionString.indexOf(token) != -1) {
				return true;
			}
			return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	public JSONArray getMethodArray(HttpServletRequest request) {
		try {
			// &map_acts =
			// [{t:1469400255523,b:"LABT|0",a:"move",x:3,y:7},
			//{t:1469400235386,b:"BARK|2",a:"boost_upgrade"},....]
			if(request.getParameter("map_acts")!=null){
				JSONArray ja = JSONArray.fromObject(request.getParameter("map_acts"));
				for(int i=0;i<ja.size();i++){
					JSONObject jo = (JSONObject)ja.get(i);
				}
				return ja;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
